﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.IO;

namespace DesktopGarden.Classes
{
    [Serializable]
    public class FlowerInstance : Flower
    {

        /// <summary>
        /// Start of Plants Growing season
        /// </summary>
        public string GrowingPeriodStart;

        /// <summary>
        /// End of Plants Growing Season
        /// </summary>
        public string GrowingPeriodEnd;

        /// <summary>
        /// Quality of the flowers produced
        /// </summary>
        public string BloomProductionQuality { get; set; }

        /// <summary>
        /// Number of plants planted
        /// </summary>
        public int NumberPlanted { get; set; }

        /// <summary>
        /// Store plants were bought
        /// </summary>
        public string BoughtFrom { get; set; }

        /// <summary>
        /// Date item was bought
        /// </summary>
        public DateTime DateBought { get; set; }


        /// <summary>
        /// Date plant was planted
        /// </summary>
        public DateTime DatePlanted { get; set; }

     

        public string ColorHex { get; set; }

        public string FlowerColor { get; set; }
        /// <summary>
        /// growing notes on the plant
        /// </summary>
        public string Notes { get; set; }

        public System.Drawing.Image Image
        {
            get
            {
                if (imageBinary == null) return null;

                byte[] buffer = imageBinary.ToArray();
                MemoryStream memStream = new MemoryStream();
                memStream.Write(buffer, 0, buffer.Length);
                return System.Drawing.Image.FromStream(memStream);
            }
        }

        public DateTime DateRemoved { get; set; }

        public List<PlantedFlower> PlantedFlowers { get; set; }

        List<PlantedFlower> TempAddedFlowers = new List<PlantedFlower>();

        List<PlantedFlower> TempDeletedFlowers = new List<PlantedFlower>(); 

        public bool Equals(FlowerInstance other)
        {

            //Check whether the compared object is null. 
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data. 
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the products' properties are equal. 
            return this.Name == other.Name && this.Color == other.Color;
        }

        public FlowerInstance()
        {
            PlantedFlowers = new List<PlantedFlower>();
        }

        public void AddPlantedFlower(int row, int column)
        {
            PlantedFlower fl = new PlantedFlower() { Row = row, Column = column };
            TempAddedFlowers.Add(fl);
        }

        public void RemovePlantedFlower(int row, int column)
        {
            PlantedFlower fl = (from f in PlantedFlowers
                                where f.Column == column && f.Row == row
                                select f).FirstOrDefault();
            if (fl != null)
            {
                TempDeletedFlowers.Add(fl);
                PlantedFlowers.Remove(fl);
            }
        }

        public bool HasFlowersInTiles(int row, int col)
        {
            return ((from f in PlantedFlowers
                     where f.Column == col && f.Row == row
                     select f).Count() > 0);
        }


        public void SaveAddedFlowers()
        {
            foreach (PlantedFlower f in TempAddedFlowers)
            {
                PlantedFlowers.Add(f);
            }

            TempAddedFlowers.Clear();
        }

        public void CancelAddedFlowers()
        {
            PlantedFlowers.Clear();
        }

        public void SaveDeletedFlowers()
        {
            TempDeletedFlowers.Clear();
        }

        public void CancelDeletedFlowers()
        {
            foreach (PlantedFlower f in TempDeletedFlowers)
            {
                PlantedFlowers.Add(f);
            }

            TempDeletedFlowers.Clear();
        }

        public  Brush Color
        {
            get
            {
                if (ColorHex != null)
                    return (Brush)new System.Windows.Media.BrushConverter().ConvertFromString(ColorHex);
                else
                    return null;
            }
        }

        public void SetColor(Brush b)
        {
            ColorHex = b.ToString(); 
        }


        public void SetImage(string file)
        {
            FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fileStream.Length];
            fileStream.Read(buffer, 0, (int)fileStream.Length);
            fileStream.Close();
            imageBinary = buffer;
        }


        public Byte[] imageBinary { get; set; }
    }

}
